package com.mentaleye.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mentaleye.biz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.DictionaryBean;
import com.mentaleye.bean.OaClassBean;
import com.mentaleye.bean.OaClassRelationBean;
import com.mentaleye.bean.TeacherBean;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;

@Controller
@RequestMapping(value = "/oaclass")
public class OaClassController {

	@Autowired private OaClassBiz oaClassBiz;
	@Autowired private DictionaryBiz dictionaryBiz;
	@Autowired private OaClassRelationBiz oaClassRelationBiz;
	@Autowired private TeacherBiz teacherBiz;
	@Autowired private TimeTableBiz timeTableBiz;
	@Autowired private GroupBiz groupBiz;
	@Autowired private GroupPersonBiz groupPersonBiz;
	
	@RequestMapping(value = "/oaClassList")
	public ModelAndView list(){
		ModelAndView view = new ModelAndView("ea/oaclass/oaClassList");
		Map<Integer,String>   classTypeMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CURRICULUM_TYPE.getCode());
		Map<Integer, String> classStateMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CLASS_STATE.getCode());
		view.addObject("classTypeMap", classTypeMap);
		view.addObject("classStateMap", classStateMap);
		view.addObject("classType" ,JSONObject.toJSONString(classTypeMap));
		view.addObject("classState" ,JSONObject.toJSONString(classStateMap));
		return view;
	}
	
	@RequestMapping(value = "/oaClassListData")
	@ResponseBody
	public String oaClassListDate(CommonPage commonPage){
		oaClassBiz.queryOaclassList(commonPage);
		return commonPage.toSHowPage();
	}
	
	@RequestMapping(value = "/oaClassEdit")
	public ModelAndView oaClassEdit(@RequestParam(value = "oaclassId",required = false) Integer oaclassId){
		ModelAndView view = new ModelAndView("ea/oaclass/oaClassEdit");
		Map<Integer,String>   classTypeMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CURRICULUM_TYPE.getCode());
		Map<Integer, String> classStateMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CLASS_STATE.getCode());
		view.addObject("classTypeMap", classTypeMap);
		view.addObject("classStateMap", classStateMap);
		if(oaclassId!=null){
			OaClassBean oaclass = oaClassBiz.queryOaclassData(oaclassId, null);
			view.addObject("oaclass", oaclass);
		}
		return view;
	}
	
	@RequestMapping(value = "/save")
	public @ResponseBody String save(OaClassBean oaClassBean){
		return JSONObject.toJSONString(oaClassBiz.oaClassEdit(oaClassBean));
	} 
	
	@RequestMapping(value = "/delete")
	public @ResponseBody String delete(@RequestBody List<Integer> ids){
		return JSONObject.toJSONString(oaClassBiz.oaClassDrop(ids));
	}
	
	@RequestMapping(value = "/oaClassShow")
	public ModelAndView show(@RequestParam(value = "oaclassId") Integer oaclassId,
			@RequestParam(value = "isOaclass",required = false) String isOaclass){
		ModelAndView view = new ModelAndView("ea/oaclass/oaClassShow");
		Map<Integer,String>   classStep=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.CURRICULUM_STEP.getCode());
		view.addObject("classStepMap", classStep);
		List<DictionaryBean> dictionarys = dictionaryBiz.queryAllDictionary();
		view.addObject("dics",dictionarys);
		if(isOaclass!=null){
			view.addObject("isOaclass",true);
		}else{
			view.addObject("isOaclass",false);
		}
		view.addObject("timeTableList", timeTableBiz.queryByClassId(oaclassId));
		OaClassBean oaClassBean = new OaClassBean();
		oaClassBean.setOaclassId(oaclassId);
		OaClassBean classBean = oaClassBiz.queryOaclassData(oaClassBean);
		view.addObject("oaclass",classBean);
		List<OaClassRelationBean> teacherOaclass = oaClassRelationBiz.queryOaClassRelationByIdAndPersonType(oaclassId, 24);
		view.addObject("teacherOaclass", teacherOaclass);
		List<Integer> teaids = new ArrayList<Integer>();
		if(teacherOaclass!=null){
			for (OaClassRelationBean teaid : teacherOaclass) {
				teaids.add(teaid.getPersonId());
			}
		}
		if(teaids!=null && teaids.size()>0){
			List<TeacherBean> teachers = teacherBiz.queryTeacherByIdList(teaids);
			view.addObject("teachers",teachers);
		}
		view.addObject("groupList",groupBiz.queryByParentId(oaclassId));
		view.addObject("personList",groupPersonBiz.queryPersonList(oaclassId,true));
		view.addObject("notPersonList",groupPersonBiz.queryPersonList(oaclassId,false));
		return view;
	}

	@RequestMapping("/checkClassByName")
	@ResponseBody
	public String getClassByName(@RequestParam String className){
		CommonResult result = this.oaClassBiz.getCountByName(className);
		return JSONObject.toJSONString(result);
	}
}
